package ooday04;

public class Test {
    public static void main(String[] args) {
        Animal[] animals = new Animal[3];//创建Animal数组对象
        animals[0] = new Dog("小黑",10,"黑");//向上造型
        animals[1] = new Chick("坤坤",25,"黑");
        animals[2] = new Fish("德华",16,"黄");
        for(int i = 0;i<animals.length;i++){//遍历所有动物
            animals[i].eat();//小狗小鸡小鱼都吃饭//超类里有的的方法不用强转就直接调方法
            animals[i].drink();//小狗小鸡小鱼都喝水
            System.out.println(animals[i].name);//输出小狗小鸡小鱼的名字
            if(animals[i] instanceof Dog){//遍历如果animals[i]强转Dog为true，就走if方法体
                Dog dog = (Dog) animals[i];//Dog强转animals
                dog.lookHome();//调用方法
            }
            if(animals[i] instanceof Chick){//若为鸡
                Chick chick = (Chick) animals[i];
                chick.layEggs();
            }
            if(animals[i] instanceof Fish){//若为鱼
                Fish fish = (Fish) animals[i];
            }
            if(animals[i] instanceof Swim){//若能游泳
                Swim sw = (Swim) animals[i];
                sw.swim();
            }
        }
        /*
        Master master = new Master();
        Animal dog = new Dog("小黑",1,"黑");
        Animal chick = new Chick("坤坤",2,"白");
        Animal fish = new Fish("德华",3,"灰");
        master.feed(dog);//在传参的同时，系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);

         */

//        Dog[] dogs = new Dog[3];
//        dogs[0] = new Dog("小黑",1,"黑");
//        dogs[1] = new Dog("小白",2,"白");
//        dogs[2] = new Dog("小灰",3,"灰");
//        for(int i = 0;i<dogs.length;i++){
//            System.out.println(dogs[i].name);//输出所有小狗的名字
//            dogs[i].swim();//所有小狗游泳
//        }
//
//
//        Chick[] chicks = new Chick[2];
//        chicks[0] = new Chick("棕棕",10,"棕");
//        chicks[1] = new Chick("橙",11,"橙");
//        chicks[0].name = "渣渣";//给第一只鸡改名
//        chicks[0].color = "灰";//给第一只鸡改颜色
//
//
//
//        Fish[] fish = new Fish[3];
//        fish[0] = new Fish("金金",1,"金");
//        fish[1] = new Fish("白白",2,"白");
//        fish[2] = new Fish("黑黑",3,"黑");
//        fish[0].swim();
//        fish[1].eat();
//        fish[2].drink();
    }
}
